package cn.mxz.robots.province;

import java.io.File;

import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import cn.mxz.crawler_single.basic.Request;
import cn.mxz.rds.pojo.GaoKaoProvince;
import cn.mxz.rds.pojo.GaoKaoRank;
import cn.mxz.rds.service.GaoKaoProvinceService;
import cn.mxz.rds.service.GaoKaoRankService;
import cn.mxz.robots.basic.local.Exam;
import cn.mxz.robots.basic.local.IRank;
import cn.mxz.robots.pojo.Student;

/**
 * 天津高考查询
 * 排名:完成
 * 去年排名:完成
 * 
 * @author chenmin <chenmin@mouge.wang>
 * @since 2015-06-23
 */
public class Tianjin extends Exam implements IRank
{

	public Tianjin(Student student)
	{
		super(student);
	}

	public static void main(String[] args)
	{
		Student student = new Student();
		student.setStudentId("15120101973313");
		student.setIdCard("120104199702283228");
		student.setType(BEN_KE_LI_KE);
		Tianjin guangxi = new Tianjin(student);

		// System.out.println(guangxi.getCaptcha());
		// System.out.println("请输入验证码：");
		// @SuppressWarnings("resource")
		// String captcha = new Scanner(System.in).next();
		// System.out.println(guangxi.login(captcha));

		guangxi.lastYearRank();
	}

	@Override
	public String getCaptcha()
	{
		Request captchaRequest = new Request("http://111.160.75.143:8000/gaokaochafen/servlet/ImageServlet", mSha1);

		return curlFile(captchaRequest);
	}

	@Override
	public String login(String captcha)
	{
		Request loginRequest = new Request("http://111.160.75.143:8000/gaokaochafen/loginvalid.do?method=valid", mSha1);
		loginRequest.addHeader("Referer", "http://111.160.75.143:8000/gaokaochafen/");
		loginRequest.addParams("username", mStudent.getStudentId());
		loginRequest.addParams("password", mStudent.getIdCard());
		loginRequest.addParams("code", captcha);
		String loginHtml = curlPost(loginRequest);

		JSONObject resultObject = new JSONObject();
		if (loginHtml.contains("true"))
		{
			Request lastRequest = new Request("http://111.160.75.143:8000/gaokaochafen/gerenxinxi.do?method=CJ1", mSha1);

			resultObject.put("result", LOGIN_SUCCESS);
			resultObject.put("data", loginParseHtml(curlGet(lastRequest)));
		}
		else if (loginHtml.contains("验证码错误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_CAPTCHA);
		}
		else if (loginHtml.contains("考生号或身份证号错误"))
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", WRONG_USER_PASSWORD);
		}
		else
		{
			resultObject.put("result", ERROR);
			resultObject.put("data", INTERNAL_SERVER_ERROR);
		}

		return resultObject.toString();
	}

	@Override
	public String loginParseHtml(String parseHtml)
	{
		Document resultDoc = Jsoup.parse(parseHtml);
		Elements tables = resultDoc.select("table");

		JSONObject json = new JSONObject();
		json.put("exam_id", tables.get(1).select("tr").get(1).select("td").get(1).text());
		json.put("name", tables.get(1).select("tr").get(2).select("td").get(1).text());
		json.put("ranking",
				getRank(58, tables.get(2).select("tr").get(2).select("td").get(4).text().trim(), mStudent.getType()));
		json.put("province", "天津省");

		JSONObject score = new JSONObject();
		score.put("语文", tables.get(2).select("tr").get(2).select("td").get(0).text().trim());
		score.put("数学", tables.get(2).select("tr").get(2).select("td").get(1).text().trim());
		score.put("外语", tables.get(2).select("tr").get(2).select("td").get(2).text().trim());
		score.put("综合", tables.get(2).select("tr").get(2).select("td").get(3).text().trim());
		score.put("总分", tables.get(2).select("tr").get(2).select("td").get(4).text().trim());
		json.put("score", score.toString());

		return json.toString();
	}

	@Override
	public void initRank()
	{
		File file = new File("/home/chenmin/Desktop/test.html");

		Integer counter = 0;
		Document test;
		try
		{
			test = Jsoup.parse(file, "UTF-8");
			Elements trs = test.select("table").get(0).select("tr");
			for (int i = 1; i < trs.size(); i++)
			{
				Integer score = Integer.valueOf(trs.get(i).select("td").get(5).text());
				if (i == 1)
				{
					counter = Integer.valueOf(trs.get(i).select("td").get(6).text()) - 0;
				}
				else
				{
					counter = Integer.valueOf(trs.get(i).select("td").get(6).text())
							- Integer.valueOf(trs.get(i - 1).select("td").get(6).text());
				}
				Integer ranking = Integer.valueOf(trs.get(i).select("td").get(6).text());
				System.out.println(score + ":" + counter + ":" + ranking);

				GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
				GaoKaoProvince province = provinceService.get(27 + 31);
				GaoKaoRankService rankService = new GaoKaoRankService();
				System.out.println(score);
				GaoKaoRank rank = new GaoKaoRank();
				rank.setScore(score);
				rank.setCount(counter);
				rank.setType(BEN_KE_WEN_KE);
				rank.setRanking(ranking);
				rank.setGaoKaoProvince(province);
				rankService.save(rank);
				for (int j = 1; j < 5; j++)
				{
					Integer scores = score - j;
					System.out.println(scores);
					// System.out.println(scores + ":" + counter + ":" +
					// Integer.valueOf(trs.get(i+1).select("td").get(6).text()));
					rank.setScore(scores);
					rank.setCount(counter);
					rank.setType(BEN_KE_WEN_KE);
					rank.setRanking(ranking);
					rank.setGaoKaoProvince(province);
					rankService.save(rank);
				}
			}
		}
		catch (Exception e)
		{

		}
	}

	@Override
	public void lastYearRank()
	{
		GaoKaoProvinceService provinceService = new GaoKaoProvinceService();
		GaoKaoProvince province = provinceService.get(27);
		GaoKaoRankService rankService = new GaoKaoRankService();

		// 理科 http://www.gxeduw.com/news/2014/106990.html
		String rankHtml = curlGet(new Request("http://www.gxeduw.com/news/2014/106990.html"));
		// 文科 http://www.gxeduw.com/news/2014/106991.html
		Document rankDom = Jsoup.parse(rankHtml);
		Elements as = rankDom.select("table").get(0).select("tr");

		Integer prescore = 0;
		Integer preleiji = 0;
		int asSize = as.size();
		for (int i = 1; i < asSize; i++)
		{
			String scoreString = as.get(i).select("td").get(0).text();
			String leiji = regexMatch(as.get(i).select("td").get(1).text(), "(\\d+)");
			Integer leijii = Integer.parseInt(leiji);

			Integer score = Integer.parseInt(scoreString);
			if (scoreString.equals("490"))
				continue;
			Integer count = leijii - preleiji;
			Integer ranking = preleiji + 1;
			preleiji = Integer.parseInt(leiji);

			if (prescore != 0)
			{
				Integer es = prescore - 1;
				Integer bs = score;
				Integer inc = count / 5;
				for (int j = es; j > bs - 1; j--)
				{
					score = j;
					if (j != es)
					{
						ranking += inc;
					}
					count = inc;
					GaoKaoRank rank = new GaoKaoRank();

					rank.setScore(score);
					rank.setCount(count);
					rank.setType(BEN_KE_LI_KE);
					rank.setGaoKaoProvince(province);
					rank.setRanking(ranking);

					rankService.save(rank);
					System.out.println(rank);
					System.out.println("score: " + scoreString + "count: " + count + "rank: " + ranking);
				}
			}
			else
			{
				GaoKaoRank rank = new GaoKaoRank();

				rank.setScore(score);
				rank.setCount(count);
				rank.setType(BEN_KE_LI_KE);
				rank.setGaoKaoProvince(province);
				rank.setRanking(ranking);

				rankService.save(rank);
				System.out.println(rank);
				System.out.println("score: " + scoreString + "count: " + count + "rank: " + ranking);
			}
			prescore = score;
		}
	}
}
